#!/bin/bash

# 编译的环境变量
export PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:$PKG_CONFIG_PATH

# 转换UI文件到指定的输出目录
function ui_to_h() {
    local path="$1"
    local output_dir="$2"

    # 检查路径是否为空
    if [ -z "$path" ]; then
        echo "Error: No input path provided."
        return 1
    fi

    # 检查输出目录是否为空
    if [ -z "$output_dir" ]; then
        echo "Error: No output directory provided."
        return 1
    fi

    # 检查路径是否存在
    if [ ! -d "$path" ]; then
        echo "Error: The specified input path does not exist."
        return 1
    fi

    # 检查输出目录是否存在，如果不存在则创建
    mkdir -p "$output_dir"

    # 检查uic工具是否存在
    if ! command -v uic &> /dev/null; then
        echo "Error: uic command not found. Please ensure Qt is installed."
        return 1
    fi

    # 遍历指定路径下的所有.ui文件并转换
    find "$path" -type f -name "*.ui" | while read -r ui_file; do
        local base_name=$(basename "$ui_file")
        local h_file="${base_name%.ui}.h"
        uic -o "$output_dir/$h_file" "$ui_file"
        echo "Converted $ui_file to $output_dir$h_file"
    done

    echo "All UI files in $path have been converted to H files in$output_dir."
}


# 先转换UI文件为头文件先
echo -e "#############################################"
echo -e "Start generating……"
ui_to_h ui/ model/
echo -e "end generating……"
echo -e "#############################################"
# 编译程序
echo -e "Enter the homework area"
mkdir -p build 
cd build
rm -rf *
echo -e "#############################################"
cmake ..
echo -e "#############################################"
make -j4
# cmake --build . -- -j $(nproc)


